package com.application.lucky.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.UserBean;
import com.application.lucky.bean.sqliteBean.BankBean;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.TwoButtonDialog;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.qunar.im.ui.view.bigimageview.tool.utility.ui.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TIXianActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private TextView mTvAddBankBt;
    private EditText mTtTixianNum;
    private View mViewLine2;
    private TextView mTvTixianYue;
    private Button mBtTixianButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_tixian;
    }

    private void initView() {
        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mTvAddBankBt = (TextView) findViewById(R.id.tv_add_bank_bt);
        mTtTixianNum = (EditText) findViewById(R.id.tt_tixian_num);
        mViewLine2 = (View) findViewById(R.id.view_line2);
        mTvTixianYue = (TextView) findViewById(R.id.tv_tixian_yue);
        mBtTixianButton = (Button) findViewById(R.id.bt_tixian_button);

        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        mTvAddBankBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityAnim(new Intent(TIXianActivity.this, SelectBankActivity.class));
            }
        });
        mBtTixianButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commint();
            }
        });
        mTvTixianYue.setText(String.format(getString(R.string.tixianyue), UserManagers.getInstance().getBalance() + ""));

        findViewById(R.id.tv_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTtTixianNum.setText(UserManagers.getInstance().getBalance() + "");
            }
        });
        mTtTixianNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                if (s.length() > 0) {
                    if (bankBean != null) {
                        mBtTixianButton.setBackgroundResource(R.drawable.shape_button_bg);
                    }
                } else {
                    mBtTixianButton.setBackgroundResource(R.drawable.shape_button_bg_un);
                }
            }
        });
    }

    TwoButtonDialog twoButtonDialog;

    private void checkVerify() {
        UserBean userBean = UserManagers.getInstance().getUserBean();
        if (userBean != null && UserManagers.getInstance().isTourist()) {
            twoButtonDialog = new TwoButtonDialog.Build(this)
                    .disMiss(true)
                    .setMessage("提现需要实名认证！")
                    .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                        @Override
                        public void click() {
                            twoButtonDialog.dismiss();
                            startActivityAnim(new Intent(TIXianActivity.this, PerfectDataActivity.class));

                        }
                    }).setLeftButton("取消", new TwoButtonDialog.BaseDialogClickListener() {
                        @Override
                        public void click() {
                            finishAnim();
                        }
                    }).create();
            twoButtonDialog.setCancelable(false);
            twoButtonDialog.setCanceledOnTouchOutside(false);
            twoButtonDialog.show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkVerify();
    }

    private void commint() {
        String s = mTtTixianNum.getText().toString();
        int money = 0;
        if (TextUtils.isEmpty(s)) {
            ToastUtils.show("请输入金额");
            return;
        }
        try {
            money = Integer.valueOf(s);
        } catch (Exception e) {
            ToastUtils.show("金额格式不正确，请重新输入！");
        }
        if (money <= 0) {

            return;
        }
        if (bankBean == null) {
            ToastUtils.show("请选择银行！");
            return;
        }

        new RedPRequest().tixian(money, bankBean, new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> integerBaseResponse) {
                UserManagers.getInstance().loadBalance();
                ToastUtils.show("提现成功");
                finish();
            }

            @Override
            public void onFailed(int code) {
                if (code == 60005) {
                    ToastUtils.show("余额不足");
                } else {
                    ToastUtils.show("提现失败");
                }
            }
        });
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    private BankBean bankBean;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addbank(BankBean bankBean) {
        this.bankBean = bankBean;
        if (bankBean != null) {
            mTvAddBankBt.setText(bankBean.accountNamw);
        }


        String s = mTtTixianNum.getText().toString();
        int money = 0;
        if (TextUtils.isEmpty(s)) {

            return;
        }
        try {
            money = Integer.valueOf(s);
        } catch (Exception e) {
            ToastUtils.show("金额格式不正确，请重新输入！");
        }
        if (money > 0) {
            mBtTixianButton.setBackgroundResource(R.drawable.shape_button_bg);
        }
    }
}
